package com.warehouse.controller;


import com.warehouse.entity.po.AddColour;
import com.warehouse.entity.po.LoginPO;
import com.warehouse.entity.po.UpAdminPO;
import com.warehouse.entity.po.UpColour;
import com.warehouse.entity.pojo.Admin;
import com.warehouse.entity.pojo.Colour;
import com.warehouse.result.Result;
import com.warehouse.service.impl.AdminServiceImpl;
import com.warehouse.util.JwtUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 管理员 前端控制器
 * </p>
 *
 * @author 卓汉杰
 * @since 2021-05-09
 */

@Api("剥离程度")
@CrossOrigin
@RestController
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    AdminServiceImpl adminService;

    @ApiOperation(value = "登录")
    @PostMapping("/login")
    @ResponseBody
    public Result login(@RequestBody LoginPO loginPO){

        Admin admin = adminService.getById(1);

        if ( ( admin.getName() ).equals( loginPO.getName() )  && (admin.getPassworld()).equals(loginPO.getPassword())){
            Map<String,Object> data = new HashMap<>();

            data.put("token", JwtUtils.getToken(admin.getName()));

            return new Result(data,Result.OK,"登录成功");
        }else {
            return new Result(null,Result.NULL,"账号或密码不正确");
        }


    }


    @ApiOperation(value = "通过id获取管理员")
    @GetMapping("/getAdmin")
    @ResponseBody
    public Result getAdmin(){
        List<Admin> admins = new ArrayList<>();
        Admin admin = adminService.getById(1);
        admin.setPassworld("******");


        if (admin == null) {
            return new Result(null,Result.NULL,"该管理员不存在");
        }else {
            Map<String,Object> data = new HashMap<>();
            admins.add(admin);
            data.put("admin",admins);
            return new Result(data,Result.OK,"请求成功");
        }


    }

    @ApiOperation(value = "通过id获取管理员")
    @GetMapping("/getAdmin2")
    @ResponseBody
    public Result getAdmin2(){

        Admin admin = adminService.getById(1);

        admin.setPassworld("******");

        if (admin == null) {
            return new Result(null,Result.NULL,"该管理员不存在");
        }else {
            Map<String,Object> data = new HashMap<>();

            data.put("admin",admin);
            return new Result(data,Result.OK,"请求成功");
        }


    }


    @ApiOperation(value = "修改管理员")
    @PostMapping("/update")
    @ResponseBody
    public Result update(@RequestBody UpAdminPO upAdminPO){
        Admin admin2 = new Admin();

        admin2.setId(1);
        admin2.setName(upAdminPO.getName());
        admin2.setPassworld(upAdminPO.getPassword());

        Admin admin = adminService.getById(1);

        if ((admin.getPassworld()).equals(upAdminPO.getPwd())){
            boolean save = adminService.updateById(admin2);
            if(save){
                return new Result(null,Result.OK,"修改成功");
            }{
                return new Result(null,Result.NULL,"修改失败");
            }
        }else {
            return new Result(null,Result.NULL,"密码错误");
        }




    }




}

